package 黑马程序员Java基础;

import java.util.*;

/**
 * @ClassName: [DemoTest]
 * @Description:
 * @Author: [黄劲松]
 * @CreateDate: [2021-03-25]
 * @Version: [v1.0]
 */

public class DemoTest {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str;
        System.out.println("请输入出数字的字符串：");
        str = scanner.nextLine();
        String str1 = str.substring(6);
        String str2 = "";
        for (int i = 0; i < str1.length(); i++) {
            if ((str1.charAt(i) <= 9 && str1.charAt(i) >= 0) || (str1.charAt(i) >= 'a' && str1.charAt(i) < 'z')) {
                str2 += str1.charAt(i);
            }
        }
        new DemoTest().doString(str2);
    }

    public void doString(String string) {
        char[] chars = string.toCharArray();
        ArrayList lists = new ArrayList();
        TreeSet set = new TreeSet();
        for (int i = 0; i < chars.length; i++) {
            lists.add(String.valueOf(chars[i]));
            set.add(String.valueOf(chars[i]));
        }
        Collections.sort(lists);

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < lists.size(); i++) {
            sb.append(lists.get(i));
        }
        string = sb.toString();
        int max = 0;
        String maxString = "";
        ArrayList maxlist = new ArrayList();

        Iterator its = set.iterator();
        while (its.hasNext()) {
            String os = (String) its.next();
            int begin = string.indexOf(os);
            int end = string.lastIndexOf(os);
            int value = end - begin + 1;
            if (value > max) {
                max = value;
                maxString = os;
                maxlist.add(os);
            } else if (value == max) {
                maxlist.add(os);
            }
        }

        int index = 0;
        for (int i = 0; i < maxlist.size(); i++) {
            if (maxlist.get(i).equals(maxString)) {
                index = i;
                break;
            }
        }
        System.out.print("重复最多元素：[");
        for (int i = index; i < maxlist.size(); i++) {
            if (i == maxlist.size() - 1) {
                System.out.print("'" + maxlist.get(i) + "'");
            } else {
                System.out.print("'" + maxlist.get(i) + "',");
            }
        }
        System.out.println("]，重复个数：" + max);
    }
}